<?php
namespace Home\Controller;
use Common\Common\Classes\Message;
use Home\Model\ClassService;
use Home\Model\QuestionService;
use Home\Model\QuestionSetService;
use Home\Model\StudentResultService;
use Think\Controller;

class TeaQuizController extends HomeController{
    public function quiz(){
        $cla_sql = new ClassService();
        $check = $cla_sql->checkPower();            //查看权限
        if($check->status==1) {
            $classInfo = $cla_sql->classInfo();                  //班级的基本信息
            $qs_sql = new QuestionSetService();           //试卷基本信息
            $pageInfo = $qs_sql->getPageInfo();
            $quiz = $qs_sql->getAllQuiz($pageInfo["np"]);
            $sr_sql = new StudentResultService();
            for($i = 0;$i<count($quiz);$i++){
                $_POST["qus_id"] = $quiz[$i]["qus_id"];
                $quiz[$i]["passNum"] = $sr_sql->getPassNum();           //获得几个人数
                $quiz[$i]["lackNum"] = $classInfo["cla_stu_num"] - $sr_sql->getSubmitNum();     //计算缺交人数
            }
            $this->assign("quiz",$quiz);
            $this->assign("classInfo", $classInfo);
            $this->assign("pageInfo",$pageInfo);
            $this->display();
        }
        else{
            $this->error($check->message);
        }
    }
    //---------------------------------获得同课程下其他班级-----------------------------------------------
    public function getOtherClassInfo(){
        if(IS_AJAX){
            $cla_sql = new ClassService();
            $otherClassInfo = $cla_sql->getOtherClassInfo();
            $this->ajaxReturn($otherClassInfo);
        }
    }
    //---------------------------------新增试卷-----------------------------------------------
    public function addQuiz(){
        if(IS_AJAX){
            $qs_sql = new QuestionSetService();
            $data = $qs_sql->addQuiz();
            $this->ajaxReturn($data);
        }
    }
    //---------------------------------修改试卷-----------------------------------------------
    public function updateQuiz(){
        if(IS_AJAX){
            $qs_sql = new QuestionSetService();
            $data = $qs_sql->checkPower();
            if($data->status == 1) {
                $data = $qs_sql->updateQuiz();
            }
            $this->ajaxReturn($data);
        }
    }
    //---------------------------------删除试卷-----------------------------------------------
    public function deleteQuiz(){
        if(IS_AJAX) {
            $qs_sql = new QuestionSetService();
            $data = $qs_sql->checkPower();
            if($data->status == 1) {
                $data = $qs_sql->deleteQuiz();
            }
            $this->ajaxReturn($data);
        }
    }
    //---------------------------------检测试卷是否有题目-------------------------------
    public function checkQuiz(){
        if(IS_AJAX) {
            $qs_sql = new QuestionSetService();
            $data = $qs_sql->checkPower();
            if($data->status == 1) {
                $data = $qs_sql->checkQuiz();
            }
            $this->ajaxReturn($data);
        }
    }
    //---------------------------------获得其他班级的试卷基本信息----------------------------
    public function getOtherClassQuiz(){
        if(IS_AJAX){
            $qs_sql = new QuestionSetService();
            $_POST["cla_id"] = $_POST["class_cla_id"];
            $cla_sql = new ClassService();
            $data = $cla_sql->checkPower();
            if($data->status == 1) {
                $data = $qs_sql->getOtherClassQuiz();
            }
            $this->ajaxReturn($data);
        }
    }
    //---------------------------------从其他班级导入某张试卷------------------------------
    public function importQuiz(){       //需要的参数class_cla_id本班级的id,qus_id导入试卷的id
        if(IS_AJAX){
            $qs_sql = new QuestionSetService();
            $_POST["cla_id"] = $_POST["class_cla_id"];
            $cla_sql = new ClassService();
            $data = $cla_sql->checkPower();
            if($data->status == 1) {
                $data = $qs_sql->importQuiz();
            }
            $this->ajaxReturn($data);
        }
    }
    //---------------------------------显示某张试卷的所有题目--------------------------
    public function question() {
        $qus_sql = new QuestionSetService();            //用于检测权限的
        $_POST["qus_id"] = $_GET["qd"];               //给post赋值,用于权限的检测
        $quiz_field = array("class_cla_id");
        $class = $qus_sql->getQuizInfo($quiz_field);
        $data = $qus_sql->checkPower();             //用于检测权限的
        if($data->status == 1){
            $this->assign("qd", $_GET["qd"]);
            $this->assign("cad",$class["class_cla_id"]);
            $this->display();
        }
        else{
            $this->error($data->message);
        }
    }
    //---------------------------------显示某张试卷的所有题目--------------------------
    public function getQuestion(){
        $qus_sql = new QuestionSetService();            //用于检测权限的
        $data = $qus_sql->checkPower();              //用于检测权限的
        if($data->status == 1){
            $que_sql = new QuestionService();
            $que_field = array("que_id","que_type","que_content","que_num","que_item1","que_item2","que_item3","que_item4","que_item5",
                "que_item6","que_item7","que_item8","que_answer","que_score");
            $question = $que_sql->getQuizQuestion($que_field);
            $quiz_sql = new QuestionSetService();
            $quiz_field = array("qus_id","qus_name","qus_status","qus_create_time","class_cla_id");
            $quiz = $quiz_sql->getQuizInfo($quiz_field);
            $d["quiz"] = $quiz;
            $d["question"] = $question;
            $data = new Message(1,"获取基本信息成功",$d);
        }
        $this->ajaxReturn($data);
    }
    //---------------------------------为某张试卷添加题目------------------------------
    public function addQuestion(){
        if(IS_AJAX) {
            $que_sql = new QuestionService();
            $data = $que_sql->addQuestion();
            $this->ajaxReturn($data);
        }
    }
    //---------------------------------题目上移---------------------------------
    public function moveUpQuestion(){
        if(IS_AJAX) {
            $que_sql = new QuestionService();
            $data = $que_sql->checkPower();
            if($data->status == 1) {
                $data = $que_sql->moveUpQuestion();
            }
            $this->ajaxReturn($data);
        }
    }
    //---------------------------------题目下移---------------------------------
    public function moveDownQuestion(){
        if(IS_AJAX) {
            $que_sql = new QuestionService();
            $data = $que_sql->checkPower();
            if($data->status == 1) {
                $data = $que_sql->moveDownQuestion();
            }
            $this->ajaxReturn($data);
        }
    }
    //---------------------------------删除题目---------------------------------
    //特殊:题目删除要做出判断,如果是最后一题就可以直接删除,如果不是就要修改其他题目的编号!
    public function deleteQuestion(){
        if(IS_AJAX) {
            $que_sql = new QuestionService();
            $data = $que_sql->checkPower();
            if($data->status == 1) {
                $data = $que_sql->deleteQuestion();
            }
            $this->ajaxReturn($data);
        }
    }
    //---------------------------------修改题目---------------------------------
    public function updateQuestion(){
        if (IS_AJAX) {
            $que_sql = new QuestionService();
            $data = $que_sql->checkPower();     //检测权限
            if ($data->status == 1) {
                $data = $que_sql->updateQuestion();
            }
            $this->ajaxReturn($data);
        }
    }
    //---------------------------------开始或停用试卷-----------------------------
    public function changeStatusQuiz(){
        if(IS_AJAX) {
            $qs_sql = new QuestionSetService();
            $data = $qs_sql->checkPower();
            if ($data->status == 1) {
                $data = $qs_sql->changeStatusQuiz();
            }
            $this->ajaxReturn($data);
        }
    }

    public function grade() {
        $this->display();
    }
}